﻿using Android.App;
using Android.Util;
using System;
using System.Collections.Generic;
using System.Text;
using Android.Views;
using Android.Content;
using Android.Widget;
using ComApp;

namespace AndroidApp
{
    public class UserFeedback
    {
        private static UserFeedback instance;
        public static UserFeedback Instance
        {
            get
            {
                return instance ?? (instance = new UserFeedback());
            }
        }


        public void OpenFeedbackDialog(Context ctx)
        {
            Action<string, string> action = (userInfo, userContent) => this.CreateUserFeedback(ctx, userInfo, userContent);
            this.UserFeedbackEditDialog(ctx, action);
        }

        private void UserFeedbackEditDialog(Context ctx, Action<string, string> okAction)
        {
            LayoutInflater layoutInflater = LayoutInflater.From(ctx);
            View view = layoutInflater.Inflate(Resource.Layout.UserFeedbackView, null);
            var ad = new AlertDialog.Builder(ctx);

            var userInfoEditText = view.FindViewById<EditText>(Resource.Id.userInfoEditText);
            var userContentEditText = view.FindViewById<EditText>(Resource.Id.userContentEditText);

            ad.SetTitle(string.Empty);
            ad.SetView(view);
            ad.SetInverseBackgroundForced(true);
            ad.SetCancelable(true);
            ad.SetPositiveButton("取消", (sender, e) =>
            {
                ad.Dispose();
            });

            ad.SetNegativeButton("发送", (sender, e) =>
            {
                var userInfo = userInfoEditText.Text;
                var userContent = userContentEditText.Text;
                ad.Dispose();
                okAction(userInfo, userContent);
            });

            ad.Show();
        }


        private void CreateUserFeedback(Context ctx,string userInfo, string userContent)
        {
            using (var client = new WcfSystemServiceRef.WcfSystemService())
            {
                client.Url = AppUnity.Instance.ServerDeploy.WcfSystemUrl;
                client.CreateUserFeedbackCompleted += (s, e) =>
                {
                    AppUnity.Instance.RunOnUiThread(() =>
                    {
                        var fileNameBak = System.IO.Path.Combine(AppPath.Instance.AppDataPath, "FileLogBak.txt");
                        if (System.IO.File.Exists(fileNameBak))
                        {
                            Toast.MakeText(ctx, "正在上传错误报告....", ToastLength.Short).Show();
                            UploadLogFile(ctx, fileNameBak);
                        }
                        else
                        {
                            Toast.MakeText(ctx, "你的反馈是我前行的动力.", ToastLength.Short).Show();
                        }
                    });
                };
                client.CreateUserFeedbackAsync(ThisApp.Instance.AppDeploy.CurrentUser.ID.ToString(),"AndroidApp", ThisApp.Instance.AppDeploy.CurrentVer, userInfo, userContent);
                Toast.MakeText(ctx, "正在发送反馈信息....", ToastLength.Short).Show();
            }
        }

        internal void UploadLogFile(Context ctx, string filePath)
        {
            try
            {
                using (System.Net.WebClient client = new System.Net.WebClient())
                {
                    var fileName = System.IO.Path.GetFileName(filePath);
                    var uri = string.Format("http://{0}/Default/UserFeedback/UploadLogFile/{1}?qqfilename=f{2}", AppUnity.Instance.ServerDeploy.WebServerHostName, AppUnity.Instance.CurrentUser.ID, fileName);
                    client.UploadFileCompleted += (s, e) =>
                    {
                        AppUnity.Instance.RunOnUiThread(() =>
                        {
                            Toast.MakeText(ctx, "上传完成, 你的反馈是我前行的动力.", ToastLength.Short).Show();
                        });
                    };
                    client.UploadFileAsync(new Uri(uri), filePath);
                    AppUnity.Instance.LogService.Log("上传日志:" + uri);
                }
            }
            catch(Exception e)
            {
                AppUnity.Instance.LogService.Log(e);
            }
        }

    }
}
